package com.github.ecbp.common.data.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.github.ecbp.common.security.utils.CommonAdminUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 起始版本 3.3.0; 默认有值不覆盖,如果提供的值为null则填充
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 自动填充-新增数据填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "createUser", String.class, CommonAdminUtils.getLoginPhoneFromToken());
        this.strictInsertFill(metaObject, "createUserId", Long.class, CommonAdminUtils.getLoginIdFromSystem());
        this.strictInsertFill(metaObject, "deleteStatus", Boolean.class, true);
    }

    /**
     * 自动填充-更新数据填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        this.strictUpdateFill(metaObject, "updateUser", String.class, CommonAdminUtils.getLoginPhoneFromToken());
        this.strictUpdateFill(metaObject, "updateUserId", Long.class, CommonAdminUtils.getLoginIdFromSystem());
    }
}